﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;

public partial class Manage_examinfo : ManagePageBase
{
    protected int stuid = 0;
    protected int paperid = 0;
    protected string path = string.Empty;

    protected int qnumber = 0;
    protected Exam.Core.Model.ExamPaper model = new Exam.Core.Model.ExamPaper();
    protected Exam.Core.Model.StudentExam stuexammodel = new Exam.Core.Model.StudentExam();
    protected XmlNodeList stupaper;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["stuid"]))
        {
            if (!int.TryParse(Request.QueryString["stuid"].Trim(), out stuid))
            {
                Response.Write(Resources.labels.error);
                Response.End();
            }
        }
        if (!string.IsNullOrEmpty(Request.QueryString["paperid"]))
        {
            if (!int.TryParse(Request.QueryString["paperid"].Trim(), out paperid))
            {
                Response.Write(Resources.labels.error);
                Response.End();
            }
        }
        if (!string.IsNullOrEmpty(Request.QueryString["p"]))
        {
            path = Server.UrlDecode(Request.QueryString["p"]);
            path = Exam.Core.FilePath.StuExamPath + path;
        }
        if (!IsPostBack)
        {
            getPaper();//获取试卷
        }
    }
    
    private void getPaper()
    {
        model = new Exam.Core.DAL.Paper().getPaperById(paperid);
        stuexammodel = new Exam.Core.DAL.StudentExam().getStudentScoreByStuidPaperid(stuid, paperid);
        stupaper = Exam.Core.DBUtility.XmlHelper.GetXmlNodeListByXpath(path, "//root/content/questiontype");
    }

    protected string getStuAnswer(string tid,string qid)
    {
        foreach (XmlNode node in stupaper)
        {
            if (node.Attributes["id"].Value.Trim() == tid.Trim())
            {
                XmlNodeList qlist = node.ChildNodes;
                foreach (XmlNode n in qlist)
                {
                    if (n.Attributes["questionid"].Value.Trim() == qid.Trim()) //questionid="2026" answer="B" studentanswer="C" stuscore="0" 
                    {
                        string right = n.Attributes["answer"].Value.Trim();
                        string stuans = n.Attributes["studentanswer"].Value.Trim();
                        if (tid.Trim() == "3")
                        {
                            if (right.Trim().ToUpper() == "A") { right = Resources.labels.right; } else { right = Resources.labels.error; }
                            if (stuans.Trim().ToUpper() == "A") { stuans = Resources.labels.right; } else { stuans = Resources.labels.error; }
                        }
                        return Resources.labels.rightAnswer + ": " + right.Trim() + ", " + Resources.labels.stuAnswer + ": " + stuans.Trim() + ", " + Resources.labels.score + ": " + n.Attributes["stuscore"].Value.Trim()+".";
                    }
                }
            }
        }
        return "";
            
    }

    protected string getNumber(int number)
    {
        qnumber = qnumber + 1;//计算题目总数
        string result = number.ToString();
        StringBuilder sb = new StringBuilder();
        foreach (char c in result)
        {
            //<img src='../images/number/<%=getNumber(k+1) %>r.gif' border=0 />
            sb.Append("<img src='../images/number/" + c + "r.gif' border=0 />");
        }
        return sb.ToString();
    }
}